// pages/commentdetails/commentdetails.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:{},//用户信息
    placeholder:'添加评论',
    rouse:false,//是否唤醒input
    searchinput: '', // 评论框里的内容
    detail:{},//
    replyList:{},//回复列表

    circleid:'',
    userid:'',
    commentId:'',
  },
 // 跳转图书详情
 booksdetail(event) {
  var id = event.currentTarget.dataset.id
  wx.navigateTo({
    url: '/pages/booksdetail/booksdetail?id=' + id,
  })
},

      /**
   * 用户点击右上角分享
   */
  // 邀请
  onShareAppMessage: function (res) {
    console.log(res);
    var that = this;
    var id = res.target.dataset.id;
    if (res.from === 'button') {
      // 通过按钮触发
      var data = res.target.dataset
      return {
        title: '',
        path: '/pages/commentdetails/commentdetails?item='+ JSON.stringify(that.data.detail),
        // imageUrl: img,
        success: function (res) {
          // 转发成功
          console.log('转发成功')
        },
        fail: function (res) {
          // 转发失败
          console.log('转发失败')
        }
      }
    }
    //通过右上角菜单触发
    return {
      title: '小萤借书',
      path: '/pages/commentdetails/commentdetails?item='+that.data.detail,
      imageUrl: '/images/aikepler-logo.jpeg'
    };
  },



  // 删除
  del:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.id);
    wx.showModal({
      title: '提示',
      content: '是否删除该条动态',
      success (res) {
        if (res.confirm) {
          that.circleDelete(e.currentTarget.dataset.id)  //圈子删除
        } else if (res.cancel) {

        }
      }
    })
  },

  // 圈子删除
  circleDelete:function(id){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
      wx.request({
        url: getApp().url.dev + '/api/circle/circleDelete', //这里就写上后台解析手机号的接口
        //这里的几个参数是获取授权后的加密数据，作为参数传递给后台就行了
        data: {
          circleId:id
        },
        method: 'get',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
           'token': wx.getStorageSync("token"),
        },
        success(res) {
          wx.hideLoading()
          if(res.data.code == 200){
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1
              })
            },1000)
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
  },




  // input失去焦点事件
  bindblurs:function(){
    // setTimeout(()=>{
    //   this.setData({
    //     placeholder:'添加评论',
    //     circleid:'',
    //     userid:'',
    //     commentId:'',
    //     searchinput:'', 
    //   })
    // },1000)
  },
  // 点击回复唤醒弹框
  rouseise:function(e){
    console.log(e);
    console.log(e.currentTarget.dataset.commentid);
    var circleid = e.currentTarget.dataset.circleid;
    var userid = e.currentTarget.dataset.userid;
    var nickName = e.currentTarget.dataset.nickname;
    var commentId = e.currentTarget.dataset.commentid;
    var aee = '回复'+ nickName
    this.setData({
      rouse:true,
      placeholder:aee,
      circleid:circleid,
      userid:userid,
      commentId:commentId,
    })
  },

  // 回复
  binclick:function(e){
    console.log(e);
    var circleid = e.currentTarget.dataset.circleid;
    var userid = e.currentTarget.dataset.userid;
    var nickName = e.currentTarget.dataset.nickname;
    var commentId = e.currentTarget.dataset.commentid;
    var aee = '回复'+ nickName
    this.setData({
      rouse:true,
      placeholder:aee,
      circleid:circleid,
      userid:userid,
      commentId:commentId,
    })
  },

  sdsinputcheag:function(e){
    this.setData({
      searchinput: e.detail.value
    })
  },

  // 点击按钮发怂
  fasings:function(){
    if(this.data.searchinput == ''){
      wx.showToast({ title: '请输入评论或回复内容', icon: 'none', duration: 2000 })
      return;
    }
    this.addrepiy();  // // 添加评论/回复
  },
  // 评论框输入事件
  commentinput(e) {
    // console.log(e.detail.value);
    if(e.detail.value == ''){
      wx.showToast({ title: '请输入评论或回复内容', icon: 'none', duration: 2000 })
      return;
    }
    this.setData({
      searchinput: e.detail.value
    })
    this.addrepiy();  // // 添加评论/回复
  },

  // 添加评论/回复
  addrepiy:function(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: getApp().url.dev + '/api/circle/addComment', //这里就写上后台解析手机号的接口
      //这里的几个参数是获取授权后的加密数据，作为参数传递给后台就行了
      data: {
        circleId:that.data.detail.id,//圈子id
        content:that.data.searchinput,//评论/回复内容
        parentId:that.data.commentId,//要回复评论id
        replayUserId:that.data.userid,//被评论/回复用户id
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
         'token': wx.getStorageSync("token"),
      },
      success(res) {
        wx.hideLoading()
        if(res.data.code == 200){
          // 成功后讲评论内容清空
          // that.setData({
          //   searchinput:'', 
          //   circleid:'',
          //   userid:'',
          // })
          that.setData({
            placeholder:'添加评论',
            circleid:'',
            userid:'',
            commentId:'',
            searchinput:'', 
          })
          that.circleComment();  //圈子评论列表
        }else{
          wx.showToast({ title: res.msg, icon: 'none', duration: 2000 })
        }
      }
    })
  },


    // 跳转个人首页
    personalhomepage(e) {
      console.log(e.currentTarget.dataset.userid);
      if(this.data.userinfo.id == e.currentTarget.dataset.userid){
        wx.switchTab({
          url: '/pages/my/my'
        })
      }else{
        wx.navigateTo({
          url: '/pages/personalhomepage/personalhomepage?userId='+e.currentTarget.dataset.userid,
        })
      }
    },

      // 获取用户信息
      getUserInfo:function(){
        var that = this;
        wx.request({
          url: getApp().url.dev + '/api/user/getUserInfo', //这里就写上后台解析手机号的接口
          //这里的几个参数是获取授权后的加密数据，作为参数传递给后台就行了
          data: {},
          method: 'get',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
             'token': wx.getStorageSync("token"),
          },
          success(res) {
            console.log(res);

            if(res.data.code == 401){
              wx.navigateTo({
                url: '/pages/login/login'
              })
              return;
            }
            if(res.data.code == 200){
              that.setData({
                userinfo:res.data.data
              })
            }
          }
        })
      }, 



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detail:JSON.parse(options.item)
    })
    // 圈子评论列表
    this.circleComment();
    this.getUserInfo(); //获取用户信息
  },


    //圈子评论列表
    circleComment:function(){
      // console.log(e.currentTarget.dataset.likesstatus);
      var that = this;
      wx.request({
        url: getApp().url.dev + '/api/circle/circleComment', //这里就写上后台解析手机号的接口
        //这里的几个参数是获取授权后的加密数据，作为参数传递给后台就行了
        data: {
          circleId:that.data.detail.id,//圈子id
        },
        method: 'get',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
           'token': wx.getStorageSync("token"),
        },
        success(res) {
          console.log(res);
          if(res.data.code == 200){
            that.setData({
              replyList:res.data.data
            }) 
          }
        }
      })
    },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})